package com.kavenka.zhufengfm.app.modle;

import android.content.ContentValues;
import android.database.Cursor;
import com.kavenka.zhufengfm.app.provider.DatabaseContract;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by kavenka on 2015/9/25 0025.
 */
public class DiscoverCategory {

    private long id;
    private String name;
    private String title;
    private boolean isChecked;
    private int orderNum;
    private String coverPath;
    private boolean selectedSwitch;
    private boolean isFinished;
    private String contentType;

    /**
     * 解析实体类
     * @param json
     * @throws JSONException
     */
    public void parseJSON(JSONObject json) throws JSONException {
        if (json != null) {
            id = json.getLong("id");
            name = json.getString("name");
            title = json.getString("title");
            isChecked = json.optBoolean("isChecked");
            orderNum = json.getInt("orderNum");
            coverPath = json.optString("coverPath", null);
            selectedSwitch = json.optBoolean("selectedSwitch");
            isFinished = json.optBoolean("isFinished");
            contentType = json.getString("contentType");
        }
    }
    public void parserCursor(Cursor cursor){
        if (cursor != null) {
            int index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories._ID);
            if (index!=-1){
                id=cursor.getLong(index);
            }
            index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories.NAME);
            if (index!=-1){
                name=cursor.getString(index);
            }
            index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories.TITLE);
            if (index!=-1){
                title=cursor.getString(index);
            }
            index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories.IS_CHECKED);
            if (index!=-1){
                isChecked=cursor.getInt(index)==1;
            }
            index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories.ORDER_NUM);
            if (index!=-1){
                orderNum=cursor.getInt(index);
            }
            index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories.COVER_PATH);
            if (index!=-1){
                coverPath=cursor.getString(index);
            }
            index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories.SELECTED_SWITCH);
            if (index!=-1){
                selectedSwitch=cursor.getInt(index)==1;
            }
            index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories.IS_FINISHED);
            if (index!=-1){
                isChecked=cursor.getInt(index)==1;
            }
            index = cursor.getColumnIndex(DatabaseContract.DiscoverCategories.CONTENT_TYPE);
            if (index!=-1){
                contentType=cursor.getString(index);
            }
        }
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getTitle() {
        return title;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public int getOrderNum() {
        return orderNum;
    }

    public String getCoverPath() {
        return coverPath;
    }

    public boolean isSelectedSwitch() {
        return selectedSwitch;
    }

    public boolean isFinished() {
        return isFinished;
    }

    public String getContentType() {
        return contentType;
    }

    public  ContentValues prepareValues() {
        ContentValues ret=new ContentValues();
        ret.put(DatabaseContract.DiscoverCategories._ID,id);
        ret.put(DatabaseContract.DiscoverCategories.NAME, name);
        ret.put(DatabaseContract.DiscoverCategories.TITLE, title);
        ret.put(DatabaseContract.DiscoverCategories.IS_CHECKED, isChecked ? 1 : 0);
        ret.put(DatabaseContract.DiscoverCategories.ORDER_NUM, orderNum);
        ret.put(DatabaseContract.DiscoverCategories.COVER_PATH, coverPath);
        ret.put(DatabaseContract.DiscoverCategories.SELECTED_SWITCH, selectedSwitch ? 1 : 0);
        ret.put(DatabaseContract.DiscoverCategories.IS_FINISHED, isFinished ? 1 : 0);
        ret.put(DatabaseContract.DiscoverCategories.CONTENT_TYPE, contentType);
        return ret;
    }
}
